﻿//===================================================================
//
// Description: Sparqube Picture Column Lite for SharePoint 2010
// Web: http://www.sparqube.com/
//
//-------------------------------------------------------------------
//
// Copyright (C) 2012 ArtfulBits.CZ (development[at]artfulbits.cz)
// Web: http://www.artfulbits.cz/
//
//-------------------------------------------------------------------
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
// PARTICULAR PURPOSE.
//===================================================================

using System;
using System.Web.UI;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Sparqube.SharePoint.PictureColumnLite
{
	public class PictureLiteFieldControl :
		BaseFieldControl
	{
		#region Constants

		private const string c_sPath_PictureFieldContent = "~/_controltemplates/Sparqube.PictureLiteField/PictureLiteFieldContent.ascx";

		#endregion //Constants

		#region Fields

		private PictureLiteFieldContent m_content = null;
		private object m_value = null;

		#endregion //Fields

		#region Properties

		public PictureLiteField PictureLiteField
		{
			get
			{
				return (PictureLiteField)this.Field;
			}
		}

		#endregion //Properties

		#region Overrides

		public override object Value
		{
			get
			{
				return m_value;
			}
			set
			{
				m_value = value;
			}
		}

		public override void UpdateFieldValueInItem()
		{
			EnsureChildControls();
			this.Value = m_content.Value;
			base.UpdateFieldValueInItem();
		}

		protected override void CreateChildControls()
		{
			base.CreateChildControls();

			if ((this.ControlMode == SPControlMode.New)
				|| (this.ControlMode == SPControlMode.Edit))
			{
				UpdatePanel panel = new UpdatePanel();
				panel.ID = this.ID + "_Update";
				this.Controls.Add(panel);

				m_content = this.Page.LoadControl(c_sPath_PictureFieldContent) as PictureLiteFieldContent;
				if (null == m_content)
				{
					throw new Exception(string.Format("Can not load Picture Lite field control at the following path: '{0}'.", c_sPath_PictureFieldContent));
				}
				m_content.ID = panel.ID + "_Content";

				m_content.PictureField = this.PictureLiteField;
				m_content.Value = PictureLiteValue.Parse(this.ItemFieldValue);

				panel.ContentTemplateContainer.Controls.Add(m_content);
				m_content.DataBind();
			}
		}

		public override void Validate()
		{
			if ((base.ControlMode == SPControlMode.New) ||
				(base.ControlMode == SPControlMode.Edit))
			{
				base.Validate();

				if (base.IsValid)
				{
					EnsureChildControls();
					if (this.Field.Required)
					{
						PictureLiteValue value = m_content.Value;
						if (null == value)
						{
							base.ErrorMessage = SPResource.GetString("MissingRequiredField", new object[0]);
							base.IsValid = false;
						}
					}
				}
			}
		}

		protected override void RenderFieldForDisplay(HtmlTextWriter output)
		{
			PictureLiteValue pictValue = PictureLiteValue.Parse(this.ItemFieldValue);
			if ((null != pictValue) && !string.IsNullOrEmpty(pictValue.Url))
			{
				output.AddAttribute(HtmlTextWriterAttribute.Target, "_blank");
				output.AddAttribute(HtmlTextWriterAttribute.Href, pictValue.Url);
				output.RenderBeginTag(HtmlTextWriterTag.A);

				output.AddAttribute(HtmlTextWriterAttribute.Src, pictValue.Url);
				output.AddAttribute(HtmlTextWriterAttribute.Alt, "");
				output.AddAttribute(HtmlTextWriterAttribute.Class, "sq-picturelite");
                output.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "0px");
				output.AddAttribute("onload", string.Format("if(this.offsetWidth>{0})this.style.width='{0}px';", 350));

				output.RenderBeginTag(HtmlTextWriterTag.Img);
				output.RenderEndTag();

				output.RenderEndTag();
			}
			else
			{
				output.Write("&nbsp;");
			}
		}

		#endregion //Overrides

	}// PictureLiteFieldControl
}